﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using domen.Transliteration;

namespace domen
{
    class Program
    {
        static void Main(string[] args)
        {
            var domains = File.ReadAllLines(@"d:\sources\DomenChecker\domen\domen\freeEnList.txt");
            File.WriteAllLines(@"d:\sources\DomenChecker\domen\domen\lengthAndABC.en.txt", domains.SortByLenghtAndABC());
            File.WriteAllLines(@"d:\sources\DomenChecker\domen\domen\startAndEqual.en.txt", domains.SelectStartEndEqual().SortByLenghtAndABC());
            File.WriteAllLines(@"d:\sources\DomenChecker\domen\domen\startAndEqualTwo.en.txt", domains.SelectStartEndEqualTwo().SortByLenghtAndABC());
            File.WriteAllLines(@"d:\sources\DomenChecker\domen\domen\distinct.en.txt", domains.SelectDistinct().SortByLenghtAndABC());
//            File.WriteAllLines(@"d:\sources\DomenChecker\domen\domen\sortedAbc.txt", domains.SortByAbc());
//
            const string path = @"D:\\sources\\DomenChecker\\domen\\domen\\";
            var domentoCheck = File.ReadAllLines(path + "translate.txt", Encoding.GetEncodings().Where(e => e.Name.Contains("1251")).FirstOrDefault().GetEncoding());
//            var translitList = new List<string>(domentoCheck.Select(s => s.Front(TransliterationType.Gost)));

            var client = new WebClient();


            var i = 0;
            foreach (var item in domentoCheck)
            {
                Console.WriteLine(i++);
                var response =
                    Encoding.UTF8.GetString(client.DownloadData(String.Format("http://hoster.by/service/domains/aa.php?zone=by&name={0}",item)));
                if (response.Contains("1^by"))
                {
                    File.AppendAllLines(path + "freeEnList.txt", new List<string>() {item});
                }
            }

//            var freeTranslitList = (translitList.Select(
//                translitItem =>
//                new
//                    {
//                        translitItem,
//                        response =
//                    Encoding.UTF8.GetString(
//                        client.DownloadData(String.Format("http://hoster.by/service/domains/aa.php?zone=by&name={0}",
//                                                          translitItem)))
//                    }).Where(@t => @t.response.Contains("1^by")).
//                Select(@t => @t.translitItem)).ToList();

//            var freeEnList = (from enListItem in enList
//                              let response = Encoding.UTF8.GetString(client.DownloadData(String.Format("http://hoster.by/service/domains/aa.php?zone=by&name={0}", enListItem)))
//                              where response.Contains("1^by")
//                              select enListItem).ToList();
//
//            File.AppendAllLines(path + "freeTranslitList.txt", freeTranslitList);
//            File.AppendAllLines(path + "freeEnList.txt", freeEnList);

            
        }
    }
}
